
@PostMapping("/chat")
public ResponseEntity<String> chatWithAI(@RequestBody String userMessage) {
    String url = "https://api.deepseek.com/v1/chat/completions";

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.set("Authorization", "Bearer sk-e60695c75c494e81a04c84656cbda0af");

    // 使用 JSONObject 构建 JSON 请求体
    JSONObject jsonBody = new JSONObject();
    jsonBody.put("model", "deepseek-chat");

    JSONArray messagesArray = new JSONArray();
    JSONObject messageObject = new JSONObject();
    messageObject.put("role", "user");
    messageObject.put("content", userMessage);
    messagesArray.put(messageObject);

    jsonBody.put("messages", messagesArray);
    jsonBody.put("stream", false);

    HttpEntity<String> entity = new HttpEntity<>(jsonBody.toString(), headers);

    ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class);

    return ResponseEntity.ok(response.getBody());
}
